import './index.less';
import Config from './../../libs/config';
import {Link} from 'react-router';
const Brand = React.createClass({
    render(){
        const class_name = this.props['class'] ? this.props['class'].name : '';
        const school_name = this.props['school'] ? this.props['school'].name : '';
        return (
            <Link to={Config.MENUS.CLASS} className="brand">
                <img src={require('../../imgs/header/logo_default.png')} alt=""/>
                <div >
                    <p className="title">{class_name}</p>
                    <span>{school_name}</span>
                </div>
            </Link>
        )
    }
});
const Weather = React.createClass({
    getInitialState(){
        return {
            date: '',
            hour: 0,
            second: 0
        }
    },
    componentDidMount(){
        const self = this;
        require.ensure(['./../../libs/jquery-vendor'], function () {
            require('./../../libs/jquery-vendor');
            function ReadDateTimeShow() {
                var today = new Date();
                var h = today.getHours();
                var m = today.getMinutes();
                var s = today.getSeconds();
                var f = '';
                if (h == 12) {
                    f = '&nbsp;下午';
                } else if (h > 12) {
                    // h = h - 12;
                    f = '&nbsp;下午';
                } else {
                    f = '&nbsp;上午';
                }
                // add a zero in front of numbers<10
                m = checkTime(m);
                s = checkTime(s);
                self.setState({
                    hour: h + ":" + m,
                    second: s
                });
            }

            function checkTime(i) {
                if (i < 10) {
                    i = "0" + i
                }
                return i;
            }

            function findWeather() {
                var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
                $.getScript(cityUrl, function (script, textStatus, jqXHR) {
                    var citytq = remote_ip_info.city;// 获取城市
                    var url = "http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&city=" + citytq + "&day=3&dfc=3";
                    $.ajax({
                        url: url,
                        dataType: "script",
                        scriptCharset: "gbk",
                        success: function (data) {
                            var _d = window['SWther'].add.now;
                            var _w = window['SWther'].w[citytq][0];
                            var _f = _w.f1 + "_0.png";
                            var _t = _w.t1;
                            var _s = _w.s1;
                            if (new Date().getHours() > 17) {
                                _f = _w.f2 + "_1.png";
                                _t = _w.t2;
                            }
                            $('#weather-img').html('' +
                                '<img src="http://php.weather.sina.com.cn/images/yb3/180_180/' + _f + '" alt="" />' +
                                '<div class="weather-template">' +
                                '<div>' + _w.s1 + '转' + _w.s2 + '</div>' +
                                '<div>' + _w.t1 + '~' + _w.t2 + '℃</div>' +
                                '</div>');

                            var dateArr = _d.split(' ');
                            var curDayArr = dateArr[0].split('-');
                            var curDay = curDayArr[0] + '/' + curDayArr[1] + '/' + curDayArr[2];
                            self.setState({
                                week: '周' + Config.NUM_EN_CN[new Date().getDay()].cn,
                                date: curDay
                            });
                        }
                    });
                });
            }

            findWeather();
            window.refreshWeather = findWeather;
            setInterval("refreshWeather();", 50 * 60 * 1000);
            window.refreshDateTime = ReadDateTimeShow;
            setInterval("refreshDateTime();", 1000);
        })

    },
    render(){

        return (
            <div className="weather">
                <div className="weather-box">
                    <div id="weather-img" className="weather-img">
                    </div>
                    <div className="weather-date">
                        <div>
                            <div>{this.state.week}</div>
                            <div>
                                {this.state.date}
                            </div>
                        </div>
                    </div>
                    <div className="weather-time">
                        <div>
                            {this.state.hour} <span>{this.state.second}</span>
                        </div>
                    </div>

                </div>
            </div>
        )
    }
});
export default React.createClass({
    render()
    {
        const class_ = this.props;
        return (
            <div className="header">
                <Brand {...class_}></Brand>
                <Weather></Weather>
            </div>
        )
    }
})